home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-10 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  50KB  |  955 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Features,  Next: Unloading,  Prev: Repeated Loading,  Up: Loading
  20. Features
  21. ========
  22.    `provide' and `require' are an alternative to `autoload' for loading
  23. files automatically.  They work in terms of named "features".
  24. Autoloading is triggered by calling a specific function, but a feature
  25. is loaded the first time another program asks for it by name.
  26.    The use of named features simplifies the task of determining whether
  27. required definitions have been defined.  A feature name is a symbol that
  28. stands for a collection of functions, variables, etc.  A program that
  29. needs the collection may ensure that they are defined by "requiring"
  30. the feature.  If the file that contains the feature has not yet been
  31. loaded, then it will be loaded (or an error will be signaled if it
  32. cannot be loaded).  The file thus loaded must "provide" the required
  33. feature or an error will be signaled.
  34.    To require the presence of a feature, call `require' with the
  35. feature name as argument.  `require' looks in the global variable
  36. `features' to see whether the desired feature has been provided
  37. already.  If not, it loads the feature from the appropriate file.  This
  38. file should call `provide' at the top-level to add the feature to
  39. `features'.
  40.    Features are normally named after the files they are provided in so
  41. that `require' need not be given the file name.
  42.    For example, in `emacs/lisp/prolog.el', the definition for
  43. `run-prolog' includes the following code:
  44.      (defun run-prolog ()
  45.        "Run an inferior Prolog process,\
  46.       input and output via buffer *prolog*."
  47.        (interactive)
  48.        (require 'comint)
  49.        (switch-to-buffer (make-comint "prolog" prolog-program-name))
  50.        (inferior-prolog-mode))
  51. The expression `(require 'shell)' loads the file `shell.el' if it has
  52. not yet been loaded.  This ensures that `make-shell' is defined.
  53.    The `shell.el' file contains the following top-level expression:
  54.      (provide 'shell)
  55. This adds `shell' to the global `features' list when the `shell' file
  56. is loaded, so that `(require 'shell)' will henceforth know that nothing
  57. needs to be done.
  58.    When `require' is used at top-level in a file, it takes effect if
  59. you byte-compile that file (*note Byte Compilation::.).  This is in case
  60. the required package contains macros that the byte compiler must know
  61. about.
  62.    Although top-level calls to `require' are evaluated during byte
  63. compilation, `provide' calls are not.  Therefore, you can ensure that a
  64. file of definitions is loaded before it is byte-compiled by including a
  65. `provide' followed by a `require' for the same feature, as in the
  66. following example.
  67.      (provide 'my-feature)  ; Ignored by byte compiler,
  68.                             ;   evaluated by `load'.
  69.      (require 'my-feature)  ; Evaluated by byte compiler.
  70.  - Function: provide FEATURE
  71.      This function announces that FEATURE is now loaded, or being
  72.      loaded, into the current Emacs session.  This means that the
  73.      facilities associated with FEATURE are or will be available for
  74.      other Lisp programs.
  75.      The direct effect of calling `provide' is to add FEATURE to the
  76.      front of the list `features' if it is not already in the list.
  77.      The argument FEATURE must be a symbol.  `provide' returns FEATURE.
  78.           features
  79.                => (bar bish)
  80.           
  81.           (provide 'foo)
  82.                => foo
  83.           features
  84.                => (foo bar bish)
  85.      During autoloading, if the file is not completely loaded (due to an
  86.      error in the evaluation of the contents) any function definitions
  87.      or `provide' calls that occurred during the load are undone.
  88.      *Note Autoload::.
  89.  - Function: require FEATURE &optional FILENAME
  90.      This function checks whether FEATURE is present in the current
  91.      Emacs session (using `(featurep FEATURE)'; see below).  If it is
  92.      not, then `require' loads FILENAME with `load'.  If FILENAME is
  93.      not supplied, then the name of the symbol FEATURE is used as the
  94.      file name to load.
  95.      If FEATURE is not provided after the file has been loaded, Emacs
  96.      will signal the error `error' (with data `Required feature FEATURE
  97.      was not provided').
  98.  - Function: featurep FEATURE
  99.      This function returns `t' if FEATURE has been provided in the
  100.      current Emacs session (i.e., FEATURE is a member of `features'.)
  101.  - Variable: features
  102.      The value of this variable is a list of symbols that are the
  103.      features loaded in the current Emacs session.  Each symbol was put
  104.      in this list with a call to `provide'.  The order of the elements
  105.      in the `features' list is not significant.
  106. File: elisp,  Node: Unloading,  Next: Hooks for Loading,  Prev: Features,  Up: Loading
  107. Unloading
  108. =========
  109.    You can discard the functions and variables loaded by a library to
  110. reclaim memory for other Lisp objects.  To do this, use the function
  111. `unload-feature':
  112.  - Command: unload-feature FEATURE
  113.      This command unloads the library that provided feature FEATURE.
  114.      It undefines all functions and variables defined with `defvar',
  115.      `defmacro', `defconst', `defsubst' and `defalias' by the library
  116.      which provided feature FEATURE.  It then restores any autoloads
  117.      associated with those symbols.
  118.    The `unload-feature' function is written in Lisp; its actions are
  119. based on the variable `load-history'.
  120.  - Variable: load-history
  121.      This variable's value is an alist connecting library names with the
  122.      names of functions and variables they define, the features they
  123.      provide, and the features they require.
  124.      Each element is a list and describes one library.  The CAR of the
  125.      list is the name of the library, as a string.  The rest of the
  126.      list is composed of these kinds of objects:
  127.         * Symbols, which were defined as functions or variables.
  128.         * Lists of the form `(require . FEATURE)' indicating the
  129.           features that are required.
  130.         * Lists of the form `(provide . FEATURE)' indicating the
  131.           features that are provided.
  132.      The value of `load-history' may have one element whose CAR is
  133.      `nil'.  This element describes definitions made with `eval-buffer'
  134.      on a buffer that is not visiting a file.
  135.    The command `eval-region' updates `load-history', but does so by
  136. adding the symbols defined to the element for the file being visited,
  137. rather than replacing that element.
  138. File: elisp,  Node: Hooks for Loading,  Prev: Unloading,  Up: Loading
  139. Hooks for Loading
  140. =================
  141.    You can ask for code to be executed if and when a particular library
  142. is loaded, by calling `eval-after-load'.
  143.  - Function: eval-after-load LIBRARY FORM
  144.      This function arranges to evaluate FORM at the end of loading the
  145.      library LIBRARY, if and when LIBRARY is loaded.
  146.      The library name LIBRARY must exactly match the argument of
  147.      `load'.  To get the proper results when an installed library is
  148.      found by searching `load-path', you should not include any
  149.      directory names in LIBRARY.
  150.      An error in FORM does not undo the load, but does prevent
  151.      execution of the rest of FORM.
  152.  - Variable: after-load-alist
  153.      An alist of expressions to evaluate if and when particular
  154.      libraries are loaded.  Each element looks like this:
  155.           (FILENAME FORMS...)
  156.      The function `load' checks `after-load-alist' in order to
  157.      implement `eval-after-load'.
  158. File: elisp,  Node: Byte Compilation,  Next: Debugging,  Prev: Loading,  Up: Top
  159. Byte Compilation
  160. ****************
  161.    GNU Emacs Lisp has a "compiler" that translates functions written in
  162. Lisp into a special representation called "byte-code" that can be
  163. executed more efficiently.  The compiler replaces Lisp function
  164. definitions with byte-code.  When a byte-code function is called, its
  165. definition is evaluated by the "byte-code interpreter".
  166.    Because the byte-compiled code is evaluated by the byte-code
  167. interpreter, instead of being executed directly by the machine's
  168. hardware (as true compiled code is), byte-code is completely
  169. transportable from machine to machine without recompilation.  It is not,
  170. however, as fast as true compiled code.
  171.    In general, any version of Emacs can run byte-compiled code produced
  172. by recent earlier versions of Emacs, but the reverse is not true.  In
  173. particular, if you compile a program with Emacs 18, you can run the
  174. compiled code in Emacs 19, but not vice versa.
  175.    *Note Compilation Errors::, for how to investigate errors occurring
  176. in byte compilation.
  177. * Menu:
  178. * Compilation Functions::       Byte compilation functions.
  179. * Eval During Compile::      Code to be evaluated when you compile.
  180. * Byte-Code Objects::        The data type used for byte-compiled functions.
  181. * Disassembly::                 Disassembling byte-code; how to read byte-code.
  182. File: elisp,  Node: Compilation Functions,  Next: Eval During Compile,  Up: Byte Compilation
  183. The Compilation Functions
  184. =========================
  185.    You can byte-compile an individual function or macro definition with
  186. the `byte-compile' function.  You can compile a whole file with
  187. `byte-compile-file', or several files with `byte-recompile-directory'
  188. or `batch-byte-compile'.
  189.    When you run the byte compiler, you may get warnings in a buffer
  190. called `*Compile-Log*'.  These report usage in your program that
  191. suggest a problem, but are not necessarily erroneous.
  192.    Be careful when byte-compiling code that uses macros.  Macro calls
  193. are expanded when they are compiled, so the macros must already be
  194. defined for proper compilation.  For more details, see *Note Compiling
  195. Macros::.
  196.    While byte-compiling a file, any `require' calls at top-level are
  197. executed.  One way to ensure that necessary macro definitions are
  198. available during compilation is to require the file that defines them.
  199. *Note Features::.
  200.    A byte-compiled function is not as efficient as a primitive function
  201. written in C, but runs much faster than the version written in Lisp.
  202. For a rough comparison, consider the example below:
  203.      (defun silly-loop (n)
  204.        "Return time before and after N iterations of a loop."
  205.        (let ((t1 (current-time-string)))
  206.          (while (> (setq n (1- n))
  207.                    0))
  208.          (list t1 (current-time-string))))
  209.      => silly-loop
  210.      
  211.      (silly-loop 100000)
  212.      => ("Thu Jan 12 20:18:38 1989"
  213.          "Thu Jan 12 20:19:29 1989")  ; 51 seconds
  214.      
  215.      (byte-compile 'silly-loop)
  216.      => [Compiled code not shown]
  217.      
  218.      (silly-loop 100000)
  219.      => ("Thu Jan 12 20:21:04 1989"
  220.          "Thu Jan 12 20:21:17 1989")  ; 13 seconds
  221.    In this example, the interpreted code required 51 seconds to run,
  222. whereas the byte-compiled code required 13 seconds.  These results are
  223. representative, but actual results will vary greatly.
  224.  - Function: byte-compile SYMBOL
  225.      This function byte-compiles the function definition of SYMBOL,
  226.      replacing the previous definition with the compiled one.  The
  227.      function definition of SYMBOL must be the actual code for the
  228.      function; i.e., the compiler does not follow indirection to
  229.      another symbol.  `byte-compile' does not compile macros.
  230.      `byte-compile' returns the new, compiled definition of SYMBOL.
  231.           (defun factorial (integer)
  232.             "Compute factorial of INTEGER."
  233.             (if (= 1 integer) 1
  234.               (* integer (factorial (1- integer)))))
  235.                => factorial
  236.           
  237.           (byte-compile 'factorial)
  238.                =>
  239.           #[(integer)
  240.             "^H\301U\203^H^@\301\207\302^H\303^HS!\"\207"
  241.             [integer 1 * factorial]
  242.             4 "Compute factorial of INTEGER."]
  243.      The result is a compiled function object.  The string it contains
  244.      is the actual byte-code; each character in it is an instruction.
  245.      The vector contains all the constants, variable names and function
  246.      names used by the function, except for certain primitives that are
  247.      coded as special instructions.
  248.  - Command: compile-defun
  249.      This command reads the defun containing point, compiles it, and
  250.      evaluates the result.  If you use this on a defun that is actually
  251.      a function definition, the effect is to install a compiled version
  252.      of that function.
  253.  - Command: byte-compile-file FILENAME
  254.      This function compiles a file of Lisp code named FILENAME into a
  255.      file of byte-code.  The output file's name is made by appending
  256.      `c' to the end of FILENAME.
  257.      Compilation works by reading the input file one form at a time.
  258.      If it is a definition of a function or macro, the compiled
  259.      function or macro definition is written out.  Other forms are
  260.      batched together, then each batch is compiled, and written so that
  261.      its compiled code will be executed when the file is read.  All
  262.      comments are discarded when the input file is read.
  263.      This command returns `t'.  When called interactively, it prompts
  264.      for the file name.
  265.           % ls -l push*
  266.           -rw-r--r--  1 lewis     791 Oct  5 20:31 push.el
  267.           
  268.           (byte-compile-file "~/emacs/push.el")
  269.                => t
  270.           
  271.           % ls -l push*
  272.           -rw-r--r--  1 lewis     791 Oct  5 20:31 push.el
  273.           -rw-rw-rw-  1 lewis     638 Oct  8 20:25 push.elc
  274.  - Command: byte-recompile-directory DIRECTORY FLAG
  275.      This function recompiles every `.el' file in DIRECTORY that needs
  276.      recompilation.  A file needs recompilation if a `.elc' file exists
  277.      but is older than the `.el' file.
  278.      If a `.el' file exists, but there is no corresponding `.elc' file,
  279.      then FLAG is examined.  If it is `nil', the file is ignored.  If
  280.      it is non-`nil', the user is asked whether the file should be
  281.      compiled.
  282.      The returned value of this command is unpredictable.
  283.  - Function: batch-byte-compile
  284.      This function runs `byte-compile-file' on the files remaining on
  285.      the command line.  This function must be used only in a batch
  286.      execution of Emacs, as it kills Emacs on completion.  An error in
  287.      one file does not prevent processing of subsequent files.  (The
  288.      file which gets the error will not, of course, produce any
  289.      compiled code.)
  290.           % emacs -batch -f batch-byte-compile *.el
  291.  - Function: byte-code CODE-STRING DATA-VECTOR MAX-STACK
  292.      This function actually interprets byte-code.  A byte-compiled
  293.      function is actually defined with a body that calls `byte-code'.
  294.      Don't call this function yourself.  Only the byte compiler knows
  295.      how to generate valid calls to this function.
  296.      In newer Emacs versions (19 and up), byte-code is usually executed
  297.      as part of a compiled function object, and only rarely as part of
  298.      a call to `byte-code'.
  299. File: elisp,  Node: Eval During Compile,  Next: Byte-Code Objects,  Prev: Compilation Functions,  Up: Byte Compilation
  300. Evaluation During Compilation
  301. =============================
  302.    These features permit you to write code to be evaluated during
  303. compilation of a program.
  304.  - Special Form: eval-and-compile BODY
  305.      This form marks BODY to be evaluated both when you compile the
  306.      containing code and when you run it (whether compiled or not).
  307.      You can get a similar result by putting BODY in a separate file
  308.      and referring to that file with `require'.  Using `require' is
  309.      preferable if there is a substantial amount of code to be executed
  310.      in this way.
  311.  - Special Form: eval-when-compile BODY
  312.      This form marks BODY to be evaluated at compile time *only*.  The
  313.      result of evaluation by the compiler becomes a constant which
  314.      appears in the compiled program.  When the program is interpreted,
  315.      not compiled at all, BODY is evaluated normally.
  316.      At top-level, this is analogous to the Common Lisp idiom
  317.      `(eval-when (compile) ...)'.  Elsewhere, the Common Lisp `#.'
  318.      reader macro (but not when interpreting) is closer to what
  319.      `eval-when-compile' does.
  320. File: elisp,  Node: Byte-Code Objects,  Next: Disassembly,  Prev: Eval During Compile,  Up: Byte Compilation
  321. Byte-Code Objects
  322. =================
  323.    Byte-compiled functions have a special data type: they are
  324. "byte-code function objects".
  325.    Internally, a byte-code function object is much like a vector;
  326. however, the evaluator handles this data type specially when it appears
  327. as a function to be called.  The printed representation for a byte-code
  328. function object is like that for a vector, with an additional `#'
  329. before the opening `['.
  330.    In Emacs version 18, there was no byte-code function object data
  331. type; compiled functions used the function `byte-code' to run the byte
  332. code.
  333.    A byte-code function object must have at least four elements; there
  334. is no maximum number, but only the first six elements are actually used.
  335. They are:
  336. ARGLIST
  337.      The list of argument symbols.
  338. BYTE-CODE
  339.      The string containing the byte-code instructions.
  340. CONSTANTS
  341.      The vector of constants referenced by the byte code.
  342. STACKSIZE
  343.      The maximum stack size this function needs.
  344. DOCSTRING
  345.      The documentation string (if any); otherwise, `nil'.  For functions
  346.      preloaded before Emacs is dumped, this is usually an integer which
  347.      is an index into the `DOC' file; use `documentation' to convert
  348.      this into a string (*note Accessing Documentation::.).
  349. INTERACTIVE
  350.      The interactive spec (if any).  This can be a string or a Lisp
  351.      expression.  It is `nil' for a function that isn't interactive.
  352.    Here's an example of a byte-code function object, in printed
  353. representation.  It is the definition of the command `backward-sexp'.
  354.      #[(&optional arg)
  355.        "^H\204^F^@\301^P\302^H[!\207"
  356.        [arg 1 forward-sexp]
  357.        2
  358.        254435
  359.        "p"]
  360.    The primitive way to create a byte-code object is with
  361. `make-byte-code':
  362.  - Function: make-byte-code &rest ELEMENTS
  363.      This function constructs and returns a byte-code function object
  364.      with ELEMENTS as its elements.
  365.    You should not try to come up with the elements for a byte-code
  366. function yourself, because if they are inconsistent, Emacs may crash
  367. when you call the function.  Always leave it to the byte-compiler to
  368. create these objects; it, we hope, always makes the elements consistent.
  369.    You can access the elements of a byte-code object using `aref'; you
  370. can also use `vconcat' to create a vector with the same elements.
  371. File: elisp,  Node: Disassembly,  Prev: Byte-Code Objects,  Up: Byte Compilation
  372. Disassembled Byte-Code
  373. ======================
  374.    People do not write byte-code; that job is left to the byte compiler.
  375. But we provide a disassembler to satisfy a cat-like curiosity.  The
  376. disassembler converts the byte-compiled code into humanly readable form.
  377.    The byte-code interpreter is implemented as a simple stack machine.
  378. Values get stored by being pushed onto the stack, and are popped off and
  379. manipulated, the results being pushed back onto the stack.  When a
  380. function returns, the top of the stack is popped and returned as the
  381. value of the function.
  382.    In addition to the stack, values used during byte-code execution can
  383. be stored in ordinary Lisp variables.  Variable values can be pushed
  384. onto the stack, and variables can be set by popping the stack.
  385.  - Command: disassemble OBJECT &optional STREAM
  386.      This function prints the disassembled code for OBJECT.  If STREAM
  387.      is supplied, then output goes there.  Otherwise, the disassembled
  388.      code is printed to the stream `standard-output'.  The argument
  389.      OBJECT can be a function name or a lambda expression.
  390.      As a special exception, if this function is used interactively, it
  391.      outputs to a buffer named `*Disassemble*'.
  392.    Here are two examples of using the `disassemble' function.  We have
  393. added explanatory comments to help you relate the byte-code to the Lisp
  394. source; these do not appear in the output of `disassemble'.  These
  395. examples show unoptimized byte-code.  Nowadays byte-code is usually
  396. optimized, but we did not want to rewrite these examples, since they
  397. still serve their purpose.
  398.      (defun factorial (integer)
  399.        "Compute factorial of an integer."
  400.        (if (= 1 integer) 1
  401.          (* integer (factorial (1- integer)))))
  402.           => factorial
  403.      
  404.      (factorial 4)
  405.           => 24
  406.      
  407.      (disassemble 'factorial)
  408.           -| byte-code for factorial:
  409.       doc: Compute factorial of an integer.
  410.       args: (integer)
  411.      
  412.      0   constant 1              ; Push 1 onto stack.
  413.      
  414.      1   varref   integer        ; Get value of `integer'
  415.                                  ;   from the environment
  416.                                  ;   and push the value
  417.                                  ;   onto the stack.
  418.      
  419.      2   eqlsign                 ; Pop top two values off stack,
  420.                                  ;   compare them,
  421.                                  ;   and push result onto stack.
  422.      
  423.      3   goto-if-nil 10          ; Pop and test top of stack;
  424.                                  ;   if `nil', go to 10,
  425.                                  ;   else continue.
  426.      
  427.      6   constant 1              ; Push 1 onto top of stack.
  428.      
  429.      7   goto     17             ; Go to 17 (in this case, 1 will be
  430.                                  ;   returned by the function).
  431.      
  432.      10  constant *              ; Push symbol `*' onto stack.
  433.      
  434.      11  varref   integer        ; Push value of `integer' onto stack.
  435.      
  436.      12  constant factorial      ; Push `factorial' onto stack.
  437.      
  438.      13  varref   integer        ; Push value of `integer' onto stack.
  439.      
  440.      14  sub1                    ; Pop `integer', decrement value,
  441.                                  ;   push new value onto stack.
  442.      
  443.                                  ; Stack now contains:
  444.                                  ;   - decremented value of `integer'
  445.                                  ;   - `factorial'
  446.                                  ;   - value of `integer'
  447.                                  ;   - `*'
  448.      
  449.      15  call     1              ; Call function `factorial' using
  450.                                  ;   the first (i.e., the top) element
  451.                                  ;   of the stack as the argument;
  452.                                  ;   push returned value onto stack.
  453.      
  454.                                  ; Stack now contains:
  455.                                  ;   - result of result of recursive
  456.                                  ;        call to `factorial'
  457.                                  ;   - value of `integer'
  458.                                  ;   - `*'
  459.      
  460.      16  call     2              ; Using the first two
  461.                                  ;   (i.e., the top two)
  462.                                  ;   elements of the stack
  463.                                  ;   as arguments,
  464.                                  ;   call the function `*',
  465.                                  ;   pushing the result onto the stack.
  466.      
  467.      17  return                  ; Return the top element
  468.                                  ;   of the stack.
  469.           => nil
  470.    The `silly-loop' function is somewhat more complex:
  471.      (defun silly-loop (n)
  472.        "Return time before and after N iterations of a loop."
  473.        (let ((t1 (current-time-string)))
  474.          (while (> (setq n (1- n))
  475.                    0))
  476.          (list t1 (current-time-string))))
  477.           => silly-loop
  478.      
  479.      (disassemble 'silly-loop)
  480.           -| byte-code for silly-loop:
  481.       doc: Return time before and after N iterations of a loop.
  482.       args: (n)
  483.      
  484.      0   constant current-time-string  ; Push
  485.                                        ;   `current-time-string'
  486.                                        ;   onto top of stack.
  487.      
  488.      1   call     0              ; Call `current-time-string'
  489.                                  ;    with no argument,
  490.                                  ;    pushing result onto stack.
  491.      
  492.      2   varbind  t1             ; Pop stack and bind `t1'
  493.                                  ;   to popped value.
  494.      
  495.      3   varref   n              ; Get value of `n' from
  496.                                  ;   the environment and push
  497.                                  ;   the value onto the stack.
  498.      
  499.      4   sub1                    ; Subtract 1 from top of stack.
  500.      
  501.      5   dup                     ; Duplicate the top of the stack;
  502.                                  ;   i.e. copy the top of
  503.                                  ;   the stack and push the
  504.                                  ;   copy onto the stack.
  505.      
  506.      6   varset   n              ; Pop the top of the stack,
  507.                                  ;   and bind `n' to the value.
  508.      
  509.                                  ; In effect, the sequence `dup varset'
  510.                                  ;   copies the top of the stack
  511.                                  ;   into the value of `n'
  512.                                  ;   without popping it.
  513.      
  514.      7   constant 0              ; Push 0 onto stack.
  515.      
  516.      8   gtr                     ; Pop top two values off stack,
  517.                                  ;   test if N is greater than 0
  518.                                  ;   and push result onto stack.
  519.      
  520.      9   goto-if-nil-else-pop 17 ; Goto 17 if `n' > 0
  521.                                  ;   else pop top of stack
  522.                                  ;   and continue
  523.                                  ;   (this exits the while loop).
  524.      
  525.      12  constant nil            ; Push `nil' onto stack
  526.                                  ;   (this is the body of the loop).
  527.      
  528.      13  discard                 ; Discard result of the body
  529.                                  ;   of the loop (a while loop
  530.                                  ;   is always evaluated for
  531.                                  ;   its side effects).
  532.      
  533.      14  goto     3              ; Jump back to beginning
  534.                                  ;   of while loop.
  535.      
  536.      17  discard                 ; Discard result of while loop
  537.                                  ;   by popping top of stack.
  538.      
  539.      18  varref   t1             ; Push value of `t1' onto stack.
  540.      
  541.      19  constant current-time-string  ; Push
  542.                                        ;   `current-time-string'
  543.                                        ;   onto top of stack.
  544.      
  545.      20  call     0              ; Call `current-time-string' again.
  546.      
  547.      21  list2                   ; Pop top two elements off stack,
  548.                                  ;   create a list of them,
  549.                                  ;   and push list onto stack.
  550.      
  551.      22  unbind   1              ; Unbind `t1' in local environment.
  552.      
  553.      23  return                  ; Return value of the top of stack.
  554.      
  555.           => nil
  556. File: elisp,  Node: Debugging,  Next: Streams,  Prev: Byte Compilation,  Up: Top
  557. Debugging Lisp Programs
  558. ***********************
  559.    There are three ways to investigate a problem in an Emacs Lisp
  560. program, depending on what you are doing with the program when the
  561. problem appears.
  562.    * If the problem occurs when you run the program, you can use the
  563.      Lisp debugger to investigate what is happening during execution.
  564.    * If the problem is syntactic, so that Lisp cannot even read the
  565.      program, you can use the Emacs facilities for editing Lisp to
  566.      localize it.
  567.    * If the problem occurs when trying to compile the program with the
  568.      byte compiler, you need to know how to examine the compiler's
  569.      input buffer.
  570. * Menu:
  571. * Debugger::            How the Emacs Lisp debugger is implemented.
  572. * Syntax Errors::       How to find syntax errors.
  573. * Compilation Errors::  How to find errors that show up in byte compilation.
  574. * Edebug::        A source-level Emacs Lisp debugger.
  575.    Another useful debugging tool is a dribble file.  When a dribble file
  576. is open, Emacs copies all keyboard input characters to that file.
  577. Afterward, you can examine the file to find out what input was used.
  578. *Note Terminal Input::.
  579.    For debugging problems in terminal descriptions, the
  580. `open-termscript' function can be useful.  *Note Terminal Output::.
  581. File: elisp,  Node: Debugger,  Next: Syntax Errors,  Up: Debugging
  582. The Lisp Debugger
  583. =================
  584.    The "Lisp debugger" provides you with the ability to suspend
  585. evaluation of a form.  While evaluation is suspended (a state that is
  586. commonly known as a "break"), you may examine the run time stack,
  587. examine the values of local or global variables, or change those values.
  588. Since a break is a recursive edit, all the usual editing facilities of
  589. Emacs are available; you can even run programs that will enter the
  590. debugger recursively.  *Note Recursive Editing::.
  591. * Menu:
  592. * Error Debugging::       Entering the debugger when an error happens.
  593. * Infinite Loops::      Stopping and debugging a program that doesn't exit.
  594. * Function Debugging::    Entering it when a certain function is called.
  595. * Explicit Debug::        Entering it at a certain point in the program.
  596. * Using Debugger::        What the debugger does; what you see while in it.
  597. * Debugger Commands::     Commands used while in the debugger.
  598. * Invoking the Debugger:: How to call the function `debug'.
  599. * Internals of Debugger:: Subroutines of the debugger, and global variables.
  600. File: elisp,  Node: Error Debugging,  Next: Infinite Loops,  Up: Debugger
  601. Entering the Debugger on an Error
  602. ---------------------------------
  603.    The most important time to enter the debugger is when a Lisp error
  604. happens.  This allows you to investigate the immediate causes of the
  605. error.
  606.    However, entry to the debugger is not a normal consequence of an
  607. error.  Many commands frequently get Lisp errors when invoked in
  608. inappropriate contexts (such as `C-f' at the end of the buffer) and
  609. during ordinary editing it would be very unpleasant to enter the
  610. debugger each time this happens.  If you want errors to enter the
  611. debugger, set the variable `debug-on-error' to non-`nil'.
  612.  - User Option: debug-on-error
  613.      This variable determines whether the debugger is called when a
  614.      error is signaled and not handled.  If `debug-on-error' is `t', all
  615.      errors call the debugger.  If it is `nil', none call the debugger.
  616.      The value can also be a list of error conditions that should call
  617.      the debugger.  For example, if you set it to the list
  618.      `(void-variable)', then only errors about a variable that has no
  619.      value invoke the debugger.
  620. File: elisp,  Node: Infinite Loops,  Next: Function Debugging,  Prev: Error Debugging,  Up: Debugger
  621. Debugging Infinite Loops
  622. ------------------------
  623.    When a program loops infinitely and fails to return, your first
  624. problem is to stop the loop.  On most operating systems, you can do this
  625. with `C-g', which causes quit.
  626.    Ordinary quitting gives no information about why the program was
  627. looping.  To get more information, you can set the variable
  628. `debug-on-quit' to non-`nil'.  Quitting with `C-g' is not considered an
  629. error, and `debug-on-error' has no effect on the handling of `C-g'.
  630. Contrariwise, `debug-on-quit' has no effect on errors.
  631.    Once you have the debugger running in the middle of the infinite
  632. loop, you can proceed from the debugger using the stepping commands.
  633. If you step through the entire loop, you will probably get enough
  634. information to solve the problem.
  635.  - User Option: debug-on-quit
  636.      This variable determines whether the debugger is called when `quit'
  637.      is signaled and not handled.  If `debug-on-quit' is non-`nil',
  638.      then the debugger is called whenever you quit (that is, type
  639.      `C-g').  If `debug-on-quit' is `nil', then the debugger is not
  640.      called when you quit.  *Note Quitting::.
  641. File: elisp,  Node: Function Debugging,  Next: Explicit Debug,  Prev: Infinite Loops,  Up: Debugger
  642. Entering the Debugger on a Function Call
  643. ----------------------------------------
  644.    To investigate a problem that happens in the middle of a program, one
  645. useful technique is to cause the debugger to be entered when a certain
  646. function is called.  You can do this to the function in which the
  647. problem occurs, and then step through the function, or you can do this
  648. to a function called shortly before the problem, step quickly over the
  649. call to that function, and then step through its caller.
  650.  - Command: debug-on-entry FUNCTION-NAME
  651.      This function requests FUNCTION-NAME to invoke the debugger each
  652.      time it is called.  It works by inserting the form `(debug
  653.      'debug)' into the function definition as the first form.
  654.      Any function defined as Lisp code may be set to break on entry,
  655.      regardless of whether it is interpreted code or compiled code.
  656.      Even functions that are commands may be debugged--they will enter
  657.      the debugger when called inside a function, or when called
  658.      interactively (after the reading of the arguments).  Primitive
  659.      functions (i.e., those written in C) may not be debugged.
  660.      When `debug-on-entry' is called interactively, it prompts for
  661.      FUNCTION-NAME in the minibuffer.
  662.      Caveat: if `debug-on-entry' is called more than once on the same
  663.      function, the second call does nothing.  If you redefine a function
  664.      after using `debug-on-entry' on it, the code to enter the debugger
  665.      is lost.
  666.      `debug-on-entry' returns FUNCTION-NAME.
  667.           (defun fact (n)
  668.             (if (zerop n) 1
  669.                 (* n (fact (1- n)))))
  670.                => fact
  671.           (debug-on-entry 'fact)
  672.                => fact
  673.           (fact 3)
  674.                => 6
  675.           
  676.           ------ Buffer: *Backtrace* ------
  677.           Entering:
  678.           * fact(3)
  679.             eval-region(4870 4878 t)
  680.             byte-code("...")
  681.             eval-last-sexp(nil)
  682.             (let ...)
  683.             eval-insert-last-sexp(nil)
  684.           * call-interactively(eval-insert-last-sexp)
  685.           ------ Buffer: *Backtrace* ------
  686.           
  687.           (symbol-function 'fact)
  688.                => (lambda (n)
  689.                     (debug (quote debug))
  690.                     (if (zerop n) 1 (* n (fact (1- n)))))
  691.  - Command: cancel-debug-on-entry FUNCTION-NAME
  692.      This function undoes the effect of `debug-on-entry' on
  693.      FUNCTION-NAME.  When called interactively, it prompts for
  694.      FUNCTION-NAME in the minibuffer.
  695.      If `cancel-debug-on-entry' is called more than once on the same
  696.      function, the second call does nothing.  `cancel-debug-on-entry'
  697.      returns FUNCTION-NAME.
  698. File: elisp,  Node: Explicit Debug,  Next: Using Debugger,  Prev: Function Debugging,  Up: Debugger
  699. Explicit Entry to the Debugger
  700. ------------------------------
  701.    You can cause the debugger to be called at a certain point in your
  702. program by writing the expression `(debug)' at that point.  To do this,
  703. visit the source file, insert the text `(debug)' at the proper place,
  704. and type `C-M-x'.  Be sure to undo this insertion before you save the
  705. file!
  706.    The place where you insert `(debug)' must be a place where an
  707. additional form can be evaluated and its value ignored.  (If the value
  708. isn't ignored, it will alter the execution of the program!)  Usually
  709. this means inside a `progn' or an implicit `progn' (*note
  710. Sequencing::.).
  711. File: elisp,  Node: Using Debugger,  Next: Debugger Commands,  Prev: Explicit Debug,  Up: Debugger
  712. Using the Debugger
  713. ------------------
  714.    When the debugger is entered, it displays the previously selected
  715. buffer in one window and a buffer named `*Backtrace*' in another
  716. window.  The backtrace buffer contains one line for each level of Lisp
  717. function execution currently going on.  At the beginning of this buffer
  718. is a message describing the reason that the debugger was invoked (such
  719. as the error message and associated data, if it was invoked due to an
  720. error).
  721.    The backtrace buffer is read-only and uses a special major mode,
  722. Debugger mode, in which letters are defined as debugger commands.  The
  723. usual Emacs editing commands are available; thus, you can switch windows
  724. to examine the buffer that was being edited at the time of the error,
  725. switch buffers, visit files, or do any other sort of editing.  However,
  726. the debugger is a recursive editing level (*note Recursive Editing::.)
  727. and it is wise to go back to the backtrace buffer and exit the debugger
  728. (with the `q' command) when you are finished with it.  Exiting the
  729. debugger gets out of the recursive edit and kills the backtrace buffer.
  730.    The contents of the backtrace buffer show you the functions that are
  731. executing and the arguments that were given to them.  It also allows
  732. you to specify a stack frame by moving point to the line describing
  733. that frame.  (A stack frame is the place where the Lisp interpreter
  734. records information about a particular invocation of a function.  The
  735. frame whose line point is on is considered the "current frame".) Some
  736. of the debugger commands operate on the current frame.
  737.    The debugger itself should always be run byte-compiled, since it
  738. makes assumptions about how many stack frames are used for the debugger
  739. itself.  These assumptions are false if the debugger is running
  740. interpreted.
  741. File: elisp,  Node: Debugger Commands,  Next: Invoking the Debugger,  Prev: Using Debugger,  Up: Debugger
  742. Debugger Commands
  743. -----------------
  744.    Inside the debugger (in Debugger mode), these special commands are
  745. available in addition to the usual cursor motion commands.  (Keep in
  746. mind that all the usual facilities of Emacs, such as switching windows
  747. or buffers, are still available.)
  748.    The most important use of debugger commands is for stepping through
  749. code, so that you can see how control flows.  The debugger can step
  750. through the control structures of an interpreted function, but cannot do
  751. so in a byte-compiled function.  If you would like to step through a
  752. byte-compiled function, replace it with an interpreted definition of the
  753. same function.  (To do this, visit the source file for the function and
  754. type `C-M-x' on its definition.)
  755.      Exit the debugger and continue execution.  When continuing is
  756.      possible, it resumes execution of the program as if the debugger
  757.      had never been entered (aside from the effect of any variables or
  758.      data structures you may have changed while inside the debugger).
  759.      Continuing is possible after entry to the debugger due to function
  760.      entry or exit, explicit invocation, quitting or certain errors.
  761.      Most errors cannot be continued; trying to continue an unsuitable
  762.      error causes the same error to occur again.
  763.      Continue execution, but enter the debugger the next time any Lisp
  764.      function is called.  This allows you to step through the
  765.      subexpressions of an expression, seeing what values the
  766.      subexpressions compute, and what else they do.
  767.      The stack frame made for the function call which enters the
  768.      debugger in this way will be flagged automatically so that the
  769.      debugger will be called again when the frame is exited.  You can
  770.      use the `u' command to cancel this flag.
  771.      Flag the current frame so that the debugger will be entered when
  772.      the frame is exited.  Frames flagged in this way are marked with
  773.      stars in the backtrace buffer.
  774.      Don't enter the debugger when the current frame is exited.  This
  775.      cancels a `b' command on that frame.
  776.      Read a Lisp expression in the minibuffer, evaluate it, and print
  777.      the value in the echo area.  This is the same as the command
  778.      `M-ESC', except that `e' is not normally disabled like `M-ESC'.
  779.      Terminate the program being debugged; return to top-level Emacs
  780.      command execution.
  781.      If the debugger was entered due to a `C-g' but you really want to
  782.      quit, and not debug, use the `q' command.
  783.      Return a value from the debugger.  The value is computed by
  784.      reading an expression with the minibuffer and evaluating it.
  785.      The `r' command makes a difference when the debugger was invoked
  786.      due to exit from a Lisp call frame (as requested with `b'); then
  787.      the value specified in the `r' command is used as the value of that
  788.      frame.
  789.      You can't use `r' when the debugger was entered due to an error.
  790. File: elisp,  Node: Invoking the Debugger,  Next: Internals of Debugger,  Prev: Debugger Commands,  Up: Debugger
  791. Invoking the Debugger
  792. ---------------------
  793.    Here we describe fully the function used to invoke the debugger.
  794.  - Function: debug &rest DEBUGGER-ARGS
  795.      This function enters the debugger.  It switches buffers to a buffer
  796.      named `*Backtrace*' (or `*Backtrace*<2>' if it is the second
  797.      recursive entry to the debugger, etc.), and fills it with
  798.      information about the stack of Lisp function calls.  It then
  799.      enters a recursive edit, leaving that buffer in Debugger mode and
  800.      displayed in the selected window.
  801.      Debugger mode provides a `c' command which operates by exiting the
  802.      recursive edit, switching back to the previous buffer, and
  803.      returning to whatever called `debug'.  The `r' command also
  804.      returns from `debug'.  These are the only ways the function
  805.      `debug' can return to its caller.
  806.      If the first of the DEBUGGER-ARGS passed to `debug' is `nil' (or
  807.      if it is not one of the following special values), then the rest
  808.      of the arguments to `debug' are printed at the top of the
  809.      `*Backtrace*' buffer.  This mechanism is used to display a message
  810.      to the user.
  811.      However, if the first argument passed to `debug' is one of the
  812.      following special values, then it has special significance.
  813.      Normally, these values are passed to `debug' only by the internals
  814.      of Emacs and the debugger, and not by programmers calling `debug'.
  815.      The special values are:
  816.     `lambda'
  817.           When the first argument is `lambda', the debugger displays
  818.           `Entering:' as a line of text at the top of the buffer.  This
  819.           means that a function is being entered when
  820.           `debug-on-next-call' is non-`nil'.
  821.     `debug'
  822.           When the first argument is `debug', the debugger displays
  823.           `Entering:' just as in the `lambda' case.  However, `debug'
  824.           as the argument indicates that the reason for entering the
  825.           debugger is that a function set to debug on entry is being
  826.           entered.
  827.           In addition, `debug' as the first argument directs the
  828.           debugger to mark the function that called `debug' so that it
  829.           will invoke the debugger when exited.  (When `lambda' is the
  830.           first argument, the debugger does not do this, because it has
  831.           already been done by the interpreter.)
  832.     `t'
  833.           When the first argument is `t', the debugger displays the
  834.           following as the top line in the buffer:
  835.                Beginning evaluation of function call form:
  836.           This indicates that it was entered due to the evaluation of a
  837.           list form at a time when `debug-on-next-call' is non-`nil'.
  838.     `exit'
  839.           When the first argument is `exit', it indicates the exit of a
  840.           stack frame previously marked to invoke the debugger on exit.
  841.           The second argument given to `debug' in this case is the
  842.           value being returned from the frame.  The debugger displays
  843.           `Return value:' on the top line of the buffer, followed by
  844.           the value being returned.
  845.     `error'
  846.           When the first argument is `error', the debugger indicates
  847.           that it is being entered because an error or `quit' was
  848.           signaled and not handled, by displaying `Signaling:' followed
  849.           by the error signaled and any arguments to `signal'.  For
  850.           example,
  851.                (let ((debug-on-error t))
  852.                     (/ 1 0))
  853.                
  854.                ------ Buffer: *Backtrace* ------
  855.                Signaling: (arith-error)
  856.                  /(1 0)
  857.                ...
  858.                ------ Buffer: *Backtrace* ------
  859.           If an error was signaled, presumably the variable
  860.           `debug-on-error' is non-`nil'.  If `quit' was signaled, then
  861.           presumably the variable `debug-on-quit' is non-`nil'.
  862.     `nil'
  863.           Use `nil' as the first of the DEBUGGER-ARGS when you want to
  864.           enter the debugger explicitly.  The rest of the DEBUGGER-ARGS
  865.           are printed on the top line of the buffer.  You can use this
  866.           feature to display messages--for example, to remind yourself
  867.           of the conditions under which `debug' is called.
  868. File: elisp,  Node: Internals of Debugger,  Prev: Invoking the Debugger,  Up: Debugger
  869. Internals of the Debugger
  870. -------------------------
  871.    This section describes functions and variables used internally by the
  872. debugger.
  873.  - Variable: debugger
  874.      The value of this variable is the function to call to invoke the
  875.      debugger.  Its value must be a function of any number of arguments
  876.      (or, more typically, the name of a function).  Presumably this
  877.      function will enter some kind of debugger.  The default value of
  878.      the variable is `debug'.
  879.      The first argument that Lisp hands to the function indicates why it
  880.      was called.  The convention for arguments is detailed in the
  881.      description of `debug'.
  882.  - Command: backtrace
  883.      This function prints a trace of Lisp function calls currently
  884.      active.  This is the function used by `debug' to fill up the
  885.      `*Backtrace*' buffer.  It is written in C, since it must have
  886.      access to the stack to determine which function calls are active.
  887.      The return value is always `nil'.
  888.      In the following example, `backtrace' is called explicitly in a
  889.      Lisp expression.  When the expression is evaluated, the backtrace
  890.      is printed to the stream `standard-output': in this case, to the
  891.      buffer `backtrace-output'.  Each line of the backtrace represents
  892.      one function call.  If the arguments of the function call are all
  893.      known, they are displayed; if they are being computed, that fact
  894.      is stated.  The arguments of special forms are elided.
  895.           (with-output-to-temp-buffer "backtrace-output"
  896.             (let ((var 1))
  897.               (save-excursion
  898.                 (setq var (eval '(progn
  899.                                    (1+ var)
  900.                                    (list 'testing (backtrace))))))))
  901.           
  902.                => nil
  903.           ----------- Buffer: backtrace-output ------------
  904.             backtrace()
  905.             (list ...computing arguments...)
  906.             (progn ...)
  907.             eval((progn (1+ var) (list (quote testing) (backtrace))))
  908.             (setq ...)
  909.             (save-excursion ...)
  910.             (let ...)
  911.             (with-output-to-temp-buffer ...)
  912.             eval-region(1973 2142 #<buffer *scratch*>)
  913.             byte-code("...  for eval-print-last-sexp ...")
  914.             eval-print-last-sexp(nil)
  915.           * call-interactively(eval-print-last-sexp)
  916.           ----------- Buffer: backtrace-output ------------
  917.      The character `*' indicates a frame whose debug-on-exit flag is
  918.      set.
  919.  - Variable: debug-on-next-call
  920.      This variable determines whether the debugger is called before the
  921.      next `eval', `apply' or `funcall'.  It is automatically reset to
  922.      `nil' when the debugger is entered.
  923.      The `d' command in the debugger works by setting this variable.
  924.  - Function: backtrace-debug LEVEL FLAG
  925.      This function sets the debug-on-exit flag of the eval frame LEVEL
  926.      levels down to FLAG.  If FLAG is non-`nil', this will cause the
  927.      debugger to be entered when that frame exits.  Even a nonlocal
  928.      exit through that frame will enter the debugger.
  929.      The debug-on-exit flag is an entry in the stack frame of a
  930.      function call.  This flag is examined on every exit from a
  931.      function.
  932.      Normally, this function is only called by the debugger.
  933.  - Variable: command-debug-status
  934.      This variable records the debugging status of current interactive
  935.      command.  Each time a command is called interactively, this
  936.      variable is bound to `nil'.  The debugger can set this variable to
  937.      leave information for future debugger invocations during the same
  938.      command.
  939.      The advantage of using this variable rather that defining another
  940.      global variable is that the data will never carry over to a later
  941.      other command invocation.
  942.  - Function: backtrace-frame FRAME-NUMBER
  943.      The function `backtrace-frame' is intended for use in Lisp
  944.      debuggers.  It returns information about what computation is
  945.      happening in the eval frame LEVEL levels down.
  946.      If that frame has not evaluated the arguments yet (or is a special
  947.      form), the value is `(nil FUNCTION ARG-FORMS...)'.
  948.      If that frame has evaluated its arguments and called its function
  949.      already, the value is `(t FUNCTION ARG-VALUES...)'.
  950.      In the return value, FUNCTION is whatever was supplied as CAR of
  951.      evaluated list, or a `lambda' expression in the case of a macro
  952.      call.  If the function has a `&rest' argument, that is represented
  953.      as the tail of the list ARG-VALUES.
  954.      If the argument is out of range, `backtrace-frame' returns `nil'.
  955.